﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ClinicaDentalApp.Models.Login;
using ClinicaDentalApp.Models.Base;
using System.Web.Security;

namespace ClinicaDentalApp.Controllers
{
    public class HomeController : Controller
    {
        PersistenceModelDataContext db = new PersistenceModelDataContext();
        [HttpGet,Authorize]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        [HttpGet]
        public ActionResult LogIn()
        {
            LoginInput model = new LoginInput();
            return View(model);
        }

        public ActionResult LogIn(LoginInput model)
        {
            if (!db.DatabaseExists())
            {
                db.CreateDatabase();
            }

            Usuario user = db.Usuarios.Where(u => u.UserName == model.userName && u.Password == model.passWord).FirstOrDefault();
            if(user!= null)
            {
                model.nivelPrivilegios = user.Perfil.NivelDePrivilegios;
                FormsAuthentication.SetAuthCookie(model.userName, false);
                Session["user"] = model;
                return RedirectToAction("Index", "Home");
            }
            return View(model); 
        }
    }
}
